package com.gexing.jsondemo.ui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;

public class JsonDemoActivity extends Activity {
	private EditText et;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// try {
		// // String json =
		// "{'name':'zhangsan','age':12,'id':23423423,'is_student':true}";
		// String json =
		// "{'name':'zhangsan','age':12,'id':23423423,'is_student':true,'card_name':['sdfs','sdfs'],'car':{'color':'red'}}";
		// JSONObject jo = new JSONObject(json);
		// Person person = BeanUtils.jsonObjectToBean(Person.class, jo);
		// trace("name:" + person.getName());
		// trace("age:" + person.getAge());
		// trace("id:" + person.getId());
		// trace("is_student:" + person.isIs_student());
		// trace("card_name1:" + person.getCard_name().get(0));
		// trace("card_name1:" + person.getCard_name().get(1));
		// trace("car color:" + person.getCar().getColor());
		//
		// JSONObject beanToJsonObject = BeanUtils.beanToJsonObject(person);
		// trace("jo==null:"+(beanToJsonObject==null));
		// trace("json:"+beanToJsonObject.toString());
		//
		//
		// } catch (JSONException e) {
		// e.printStackTrace();
		// }
//		ArrayList<String> list=new ArrayList<String>();
//		list.add("1");
//		list.add("2");
//		list.add("3");
//		
//		
//		ArrayList<Person> persons = new ArrayList<Person>();
//		persons.add(new Person("gongpu1", 11, 11, true, list, new Car("red1")));
//		persons.add(new Person("gongpu2", 12, 12, true, list, new Car("red2")));
//		persons.add(new Person("gongpu3", 13, 13, true, list, new Car("red3")));
//		persons.add(new Person("gongpu4", 14, 14, true, list, new Car("red4")));
//
//		 storePerson(persons);

		restorePerson();

	}

	public void storePerson(Object obj) {
		try {
			File cacheDir = getCacheDir();
			String absolutePath = cacheDir.getAbsolutePath();
			FileOutputStream fos = new FileOutputStream(absolutePath + "/temp.out");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(obj);
			oos.flush();
			oos.close();
			fos.flush();
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void restorePerson() {
		try {
			File cacheDir = getCacheDir();
			String absolutePath = cacheDir.getAbsolutePath();
			FileInputStream fis = new FileInputStream(absolutePath + "/temp.out");
			ObjectInputStream ois = new ObjectInputStream(fis);
			ArrayList<Person> persons=(ArrayList<Person>) ois.readObject();
			for (int i = 0; i < persons.size(); i++) {
				Person person = persons.get(i);
				trace(person.toString());
			}
		} catch (Exception e) {
			exception(e);
		}
	}

	private void trace(String str) {
		Log.i("gp", str);
	}
	
	public static void exception(Exception e){
		error("异常:");
		error("Caused by:"+e.toString());
		for (StackTraceElement stackTraceElement : e.getStackTrace()) {
			error(stackTraceElement.toString());
		}
	}
	
	private static void error(String msg){
		Log.w("gp", msg);
	}
}